import axios, { AxiosRequestConfig } from 'axios'
import { baseURL } from '../config'
import {message} from "antd";

const service = axios.create({
  baseURL,
  timeout: 15000
})

export interface IResponseData {
  total: number
  size: number
  current: number
  data: any[]
}

export interface INewResponseData {
  code: number
  msg: string
  success: boolean
  data: any[]
}



service.interceptors.response.use(
  (response) => {
    const res = response.data

    if (res.code !== 200) {
        message.error(res.msg || 'Error')
        return Promise.reject(res)
    } else {
        return res
    }
  },

  (error) => {
    // if(error?.response?.status === 401 || error?.response?.status === 403){
    //     const loginUrl = getLoginUrl();
    //     window.location.replace(loginUrl);
    //     return;
    // }
    message.error(error.message || 'Error')
    return Promise.reject(error)
  }
)

// 通用的请求函数
export default async function request(config: AxiosRequestConfig) {
  return service.request<IResponseData>(config).then((res) => res.data)
}

// 通用的请求函数
export async function requestNew(config: AxiosRequestConfig) {
  return service.request<INewResponseData>(config).then((res) => res.data)
}
